<template>
    <div class="wrapper">
        <div class="box">
            <div style="font-size: 26px; margin: 20px 0; text-align: center;"><b>注 册</b></div>
            <el-form :rules="rules" :model="user" ref="userForm">
                <el-form-item prop="username">
                    <el-input size="medium" style="margin-top: 10px" placeholder="请输入用户名" v-model="user.username"
                        clearable>
                        <i slot="prefix" class="el-input__icon el-icon-user"></i>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input size="medium" style="margin-top: 10px" placeholder="请输入密码" v-model="user.password"
                        show-password clearable>
                        <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                    </el-input>
                </el-form-item>
                <el-form-item prop="confirmPassword">
                    <el-input size="medium" style="margin-top: 10px" placeholder="请确认密码" v-model="user.confirmPassword"
                        show-password clearable>
                        <i slot="prefix" class="el-input__icon el-icon-lock"></i>
                    </el-input>
                </el-form-item>

                <div style="margin:30px 0; text-align: right;">
                    <el-button type="warning" size="small" autocomplete="off" @click="login">注册</el-button>
                    <el-button size="small" autocomplete="off" @click="$router.push('/')">返回登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                user: {},
                rules: {
                    username: [{
                            required: true,
                            message: '用户名不能为空',
                            trigger: 'blur'
                        },
                        {
                            min: 3,
                            max: 10,
                            message: '请输入长度在 3 到 10 个字符',
                            trigger: 'blur'
                        }
                    ],
                    password: [{
                            required: true,
                            message: '密码不能为空',
                            trigger: 'blur'
                        },
                        {
                            min: 3,
                            max: 15,
                            message: '请输入长度在 3 到 15 个字符',
                            trigger: 'blur'
                        }
                    ],
                    confirmPassword: [{
                            required: true,
                            message: '确认密码不能为空',
                            trigger: 'blur'
                        },
                        {
                            min: 3,
                            max: 15,
                            message: '请输入长度在 3 到 15 个字符',
                            trigger: 'blur'
                        }
                    ],
                },
            }
        },
        methods: {
            login() {
                this.$refs['userForm'].validate((valid) => {
                    if (valid) {
                        if (this.user.password != this.user.confirmPassword) {
                            this.$message.error("两次输入的密码不一致！")
                            return false
                        }
                        this.request.post("/user/register", this.user).then(res => {
                            if (res.code === '200') { //表单校验合法
                                this.$message.success("注册成功！")
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                });
            },
        }
    }
</script>

<style scoped>
    .wrapper {
        height: 100vh;
        /* 渐变色背景 */
        background-image: linear-gradient(to bottom right, #879AF2, #D3208B, #FDA000);
        overflow: hidden;
    }

    .box {
        width: 350px;
        height: 360px;
        margin: 180px auto;
        background-color: #fff;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 0 1px black;
    }
</style>